<script setup lang="ts">
import { ref, onMounted, h } from 'vue';
import { Typography, Card, Statistic, Row, Col, Table, Progress, Tabs, Spin, List, Tag, Button } from 'ant-design-vue';
import {
  UserOutlined,
  TeamOutlined,
  CheckCircleOutlined,
  DashboardOutlined,
  RocketOutlined,
  LineChartOutlined,
  ApiOutlined,
  CloudOutlined
} from '@ant-design/icons-vue';

const { Title } = Typography;
const { TabPane } = Tabs;

// 统计数据
const statisticsData = ref([
  { title: '总用户数', value: 1024, prefix: UserOutlined, color: '#1668dc', increase: '12%' },
  { title: '今日活跃', value: 256, prefix: TeamOutlined, color: '#52c41a', increase: '8%' },
  { title: '任务完成率', value: 86, suffix: '%', prefix: CheckCircleOutlined, color: '#fa8c16', increase: '5%' },
  { title: '系统负载', value: 32, suffix: '%', prefix: DashboardOutlined, color: '#722ed1', decrease: '3%' }
]);

// 服务使用统计
const serviceUsage = ref([
  { name: '文本生成', value: 78, color: '#1668dc' },
  { name: '图像生成', value: 63, color: '#52c41a' },
  { name: '代码生成', value: 42, color: '#fa8c16' },
  { name: '语音合成', value: 35, color: '#722ed1' }
]);

// 类型定义
type TaskType = '文本' | '图像' | '代码' | '语音';
type TaskStatus = '已完成' | '进行中' | '等待中' | '失败';

// 颜色映射
const typeColors: Record<TaskType, string> = {
  '文本': 'blue',
  '图像': 'green',
  '代码': 'orange',
  '语音': 'purple'
};

const statusColors: Record<TaskStatus, string> = {
  '已完成': 'success',
  '进行中': 'processing',
  '等待中': 'warning',
  '失败': 'error'
};

// 表格数据
const columns = [
  {
    title: '任务ID',
    dataIndex: 'id',
    width: '15%',
  },
  {
    title: '任务名称',
    dataIndex: 'name',
    width: '25%',
  },
  {
    title: '类型',
    dataIndex: 'type',
    width: '15%',
    customRender: ({ text }: { text: TaskType }) => {
      return h(Tag, { color: typeColors[text] }, () => text);
    }
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: '15%',
    customRender: ({ text }: { text: TaskStatus }) => {
      return h(Tag, { color: statusColors[text] }, () => text);
    }
  },
  {
    title: '创建时间',
    dataIndex: 'time',
    width: '20%',
  },
  {
    title: '操作',
    width: '10%',
    customRender: () => {
      return h(Button, { type: 'link' }, () => '详情');
    }
  }
];

interface TaskItem {
  key: string;
  id: string;
  name: string;
  type: TaskType;
  status: TaskStatus;
  time: string;
}

const dataSource = ref<TaskItem[]>([
  {
    key: '1',
    id: 'TASK-1001',
    name: '智能文案生成',
    type: '文本',
    status: '已完成',
    time: '2023-10-01 08:30',
  },
  {
    key: '2',
    id: 'TASK-1002',
    name: '品牌Logo设计',
    type: '图像',
    status: '进行中',
    time: '2023-10-02 09:15',
  },
  {
    key: '3',
    id: 'TASK-1003',
    name: 'Vue组件生成',
    type: '代码',
    status: '等待中',
    time: '2023-10-03 14:20',
  },
  {
    key: '4',
    id: 'TASK-1004',
    name: '营销语音合成',
    type: '语音',
    status: '已完成',
    time: '2023-10-03 16:45',
  },
  {
    key: '5',
    id: 'TASK-1005',
    name: '数据分析报告',
    type: '文本',
    status: '失败',
    time: '2023-10-04 10:30',
  },
]);

// API调用统计
const apiCallStats = ref([
  { name: '文本API', calls: 1245, quota: 2000, icon: ApiOutlined },
  { name: '图像API', calls: 863, quota: 1000, icon: CloudOutlined },
  { name: '代码API', calls: 578, quota: 800, icon: RocketOutlined },
  { name: '分析API', calls: 320, quota: 500, icon: LineChartOutlined },
]);

// 最新动态
const activities = ref([
  { title: '系统更新', description: '新版本1.2.5已发布，包含多项功能优化', time: '刚刚' },
  { title: 'API限额调整', description: '您的月度API调用限额已提升至10,000次', time: '2小时前' },
  { title: '账户提醒', description: '您的账户将于7天后到期，请及时续费', time: '1天前' },
  { title: '新功能发布', description: '智能图像修复功能已上线，欢迎体验', time: '3天前' }
]);

// 加载状态
const loading = ref(true);

onMounted(() => {
  // 模拟加载
  setTimeout(() => {
    loading.value = false;
  }, 1000);
});
</script>

<template>
  <Spin :spinning="loading" tip="加载中..." size="large">
    <div class="dashboard">
      <div class="dashboard-header">
        <Typography>
          <Title :level="2">控制台概览</Title>
        </Typography>
        <div class="last-updated">
          最后更新: {{ new Date().toLocaleString() }}
        </div>
      </div>

      <!-- 统计卡片 -->
      <Row :gutter="[16, 16]" class="statistics-row">
        <Col :xs="24" :sm="12" :md="12" :lg="6" v-for="(stat, index) in statisticsData" :key="index">
          <Card class="stat-card" :bordered="false" :bodyStyle="{ padding: '20px' }">
            <div class="stat-card-content">
              <div class="stat-icon" :style="{ backgroundColor: stat.color }">
                <component :is="stat.prefix" />
              </div>
              <div class="stat-info">
                <div class="stat-title">{{ stat.title }}</div>
                <div class="stat-value">
                  {{ stat.value }}<span v-if="stat.suffix">{{ stat.suffix }}</span>
                </div>
                <div class="stat-trend" v-if="stat.increase || stat.decrease">
                  <span class="trend-up" v-if="stat.increase">↑ {{ stat.increase }}</span>
                  <span class="trend-down" v-if="stat.decrease">↓ {{ stat.decrease }}</span>
                </div>
              </div>
            </div>
          </Card>
        </Col>
      </Row>

      <!-- 主要内容 -->
      <Row :gutter="[16, 16]" class="main-content">
        <!-- 左侧 -->
        <Col :xs="24" :lg="16">
          <Card title="任务管理" class="task-table" :bordered="false">
            <Table 
              :columns="columns" 
              :dataSource="dataSource" 
              :pagination="{ pageSize: 5 }" 
              :rowClassName="(record, index) => index % 2 === 0 ? 'table-striped' : ''"
            >
              <template #bodyCell="{ column, text, record }">
                <template v-if="column.dataIndex === 'type'">
                  <Tag :color="typeColors[text as TaskType]">{{ text }}</Tag>
                </template>
                <template v-else-if="column.dataIndex === 'status'">
                  <Tag :color="statusColors[text as TaskStatus]">{{ text }}</Tag>
                </template>
                <template v-else-if="column.title === '操作'">
                  <Button type="link">详情</Button>
                </template>
              </template>
            </Table>
          </Card>
          
          <Card title="服务使用统计" class="service-usage" :bordered="false">
            <Row :gutter="[16, 24]">
              <Col :span="12" v-for="(service, index) in serviceUsage" :key="index">
                <div class="usage-item">
                  <div class="usage-info">
                    <div class="usage-name">{{ service.name }}</div>
                    <div class="usage-value">{{ service.value }}%</div>
                  </div>
                  <Progress 
                    :percent="service.value" 
                    :strokeColor="service.color" 
                    :showInfo="false" 
                    size="small"
                    :strokeWidth="6"
                  />
                </div>
              </Col>
            </Row>
          </Card>
        </Col>

        <!-- 右侧 -->
        <Col :xs="24" :lg="8">
          <Card class="api-usage" :bordered="false">
            <Tabs defaultActiveKey="1">
              <TabPane key="1" tab="API使用情况">
                <div class="api-stats">
                  <div v-for="(api, index) in apiCallStats" :key="index" class="api-stat-item">
                    <div class="api-icon">
                      <component :is="api.icon" />
                    </div>
                    <div class="api-info">
                      <div class="api-name">{{ api.name }}</div>
                      <Progress 
                        :percent="Math.round(api.calls/api.quota*100)" 
                        :format="() => `${api.calls}/${api.quota}`"
                        :strokeColor="api.calls/api.quota > 0.8 ? '#ff4d4f' : '#1668dc'"
                        size="small"
                      />
                    </div>
                  </div>
                </div>
              </TabPane>
              <TabPane key="2" tab="最新动态">
                <List
                  itemLayout="horizontal"
                  :dataSource="activities"
                  class="activity-list"
                >
                  <template #renderItem="{ item }">
                    <List.Item>
                      <List.Item.Meta
                        :title="item.title"
                        :description="item.description"
                      />
                      <div class="activity-time">{{ item.time }}</div>
                    </List.Item>
                  </template>
                </List>
              </TabPane>
            </Tabs>
          </Card>
          
          <Card title="快速链接" class="quick-links" :bordered="false">
            <div class="link-grid">
              <Button class="link-button">创建任务</Button>
              <Button class="link-button">查看报告</Button>
              <Button class="link-button">API文档</Button>
              <Button class="link-button">设置</Button>
            </div>
          </Card>
        </Col>
      </Row>
    </div>
  </Spin>
</template>

<style scoped>
.dashboard {
  width: 100%;
  min-height: calc(100vh - 200px); /* 设置最小高度，减去头部和底部的高度 */
  padding-bottom: 80px; /* 增加底部间距，确保内容不被底部栏遮挡 */
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.last-updated {
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;
}

.statistics-row {
  margin-bottom: 24px;
}

.stat-card {
  height: 100%;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.09);
}

.stat-card-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 16px;
  color: white;
  font-size: 24px;
}

.stat-info {
  flex: 1;
}

.stat-title {
  color: rgba(0, 0, 0, 0.65);
  font-size: 14px;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-trend {
  font-size: 12px;
}

.trend-up {
  color: #52c41a;
}

.trend-down {
  color: #ff4d4f;
}

.task-table {
  margin-bottom: 16px;
  border-radius: 8px;
  overflow: hidden;
}

.table-striped {
  background-color: rgba(0, 0, 0, 0.02);
}

.service-usage {
  margin-top: 16px;
  border-radius: 8px;
  overflow: hidden;
}

.usage-item {
  margin-bottom: 8px;
}

.usage-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}

.usage-name {
  color: rgba(0, 0, 0, 0.65);
  font-size: 14px;
}

.usage-value {
  font-weight: 500;
}

.api-usage {
  border-radius: 8px;
  overflow: hidden;
  height: 100%;
  margin-bottom: 16px;
}

.api-stats {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.api-stat-item {
  display: flex;
  align-items: center;
}

.api-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background-color: #e6f7ff;
  color: #1668dc;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  margin-right: 12px;
}

.api-info {
  flex: 1;
}

.api-name {
  margin-bottom: 4px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}

.activity-list {
  max-height: 260px;
  overflow-y: auto;
}

.activity-time {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
}

.quick-links {
  margin-top: 16px;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 80px; /* 显著增加底部边距，避免溢出 */
}

.link-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.link-button {
  height: 40px;
  border-radius: 6px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .dashboard-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .stat-card-content {
    flex-direction: column;
    text-align: center;
  }
  
  .stat-icon {
    margin-right: 0;
    margin-bottom: 12px;
  }
  
  .quick-links {
    margin-bottom: 90px; /* 移动端增加更多底部边距 */
  }
}
</style> 